<?php

namespace app\controllers;

use Yii;
use app\tools\Re;
use app\models\Customer;

class CustomerController extends BaseController {
  public function actionCustomerInfo(){
    $openid = Yii::$app->request->post('openid', '');
		$customer_info = Customer::find()->where(['openid' => $openid])->one();
		if (!$customer_info) {
			return Re::data(null, '用户信息读取错误', Re::ERROR);
		}
    return Re::data(['data' => $customer_info]);
  }

  public function actionEditCustomer(){
    $openid = Yii::$app->request->post('openid', '');
    $name = Yii::$app->request->post('name', '');
    $phone = Yii::$app->request->post('phone', '');
    $nickname = Yii::$app->request->post('nickname', '');
    $icon = Yii::$app->request->post('icon', '');
		$customer_info = Customer::find()->where(['openid' => $openid])->one();
		if (!$customer_info) {
			return Re::data(null, '用户信息读取错误', Re::ERROR);
		}
    $nickname = $nickname ?: '微信用户';
    $icon = $icon ?: Yii::$app->params['logo'];
    $customer_info->name = $name;
    $customer_info->phone = $phone;
    $customer_info->nickname = $nickname;
    $customer_info->icon = $icon;
    if (!$customer_info->save()) {
      return Re::data(null, $customer_info->showErrors(), Re::ERROR);
    }
    return Re::data(['data' => $customer_info]);
  }
}
